/*
 * reactor.h - 
 * Clunix, cezanne@clunix.com, 2005.2
 *
 * Copyright (c) 2001-2005 by Clunix Inc. All Rights Reserved.
 */

#ifndef _REACTOR_H_
#define _REACTOR_H_

#include "edbd.h"

#define REACTOR_MAGIC		0x3498ef91
#define REACTOR_FREED_MAGIC	0x91ef9834

typedef struct reactor {
#ifdef ECM_DEBUG
	ecm_uint32	magic;
#endif
	ECMBOOL		autonomous;
	ECMBOOL		valid;
	ECMBOOL		locked;
	int		refcnt;
	sr_t		*sr;
	edbd_bdexpr_t	edbd_bdexpr;
#ifdef ECM_DEBUG
	char		*LNP;
	char		*edexpr;
#endif
	ecm_cond_t		cond;
} reactor_t;

#ifdef ECM_DEBUG

char *dbg_get_reactor_info(reactor_t *reactor);

#endif

ECMBOOL reg_reactors(edbd_conn_t edbd_conn, sr_t *sr, sr_t *sr_edexprs);
void unreg_reactors(sr_t *sr);

void exec_reactor(edbd_conn_t edbd_conn, reactor_t *reactor);
void trigger_anchor(edbd_conn_t edbd_conn, sr_t *sr);

void add_autoreactor(edbd_conn_t edbd_conn, reactor_t *reactor);
void trigger_autoreactor(edbd_conn_t edbd_conn, reactor_t *reactor);
void remove_autoreactor(reactor_t *reactor);

typedef enum _xestat {
	STAT_XE_CREATE,
	STAT_XE_DELETE,
	STAT_XE_DELETE_LASTONE,
} xe_stat;

void check_reactor_from_xe(edbd_conn_t edbd_conn, sr_t *sr_homo, sr_t *sr, xe_stat xestat);

ECMBOOL exec_reactor_bdexpr(edbd_conn_t edbd_conn, sr_t *sr_anchor, sr_t *sr_args, edbd_bdexpr_t edbd_bdexpr);

ECMBOOL calc_edexpr(edbd_conn_t edbd_conn, const sr_t *sr_anchor, const sr_t *sr_edexpr);
void free_bdexpr(edbd_bdexpr_t edbd_bdexpr);

void trigger_wcardsensor_add(edbd_conn_t edbd_conn, sr_t *sr_child);
void trigger_wcardsensor_del(edbd_conn_t edbd_conn, sr_t *sr_child);
void trigger_plainsensor(edbd_conn_t edbd_conn, sr_t *sr);
void trigger_refsensor(edbd_conn_t edbd_conn, sr_t *sr);

void invalidate_lnpsensors(sr_t *sr);

void start_subtree_reactors(edbd_conn_t edbd_conn, sr_t *sr);

ECMBOOL get_reactor(reactor_t *reactor);
void put_reactor(reactor_t *reactor);

void start_master_reactors(edbd_conn_t edbd_conn, sr_t *sr);
void stop_master_reactors(edbd_conn_t edbd_conn, sr_t *sr);

void start_sub_reactors(edbd_conn_t edbd_conn, sr_t *sr);
void stop_sub_reactors(edbd_conn_t edbd_conn, sr_t *sr);

#endif
